#ifndef SERVOCONTROL_H
#define SERVOCONTROL_H

#include "LTSMC.h"
#include "PINGGER.h"

#define SMC_USE_SERIAL  (1)
#define SMC_USE_NETWORK (2)
#define SMC_ERR_ETHERCAT_PORT_NUM    (2)


#define MOVE_ABSOLUTE 1
#define MOVE_RELATIVE 0

#define ROLLING_SHUTTER_DOOR_ON_OUT_PIN   (1)
#define ROLLING_SHUTTER_DOOR_STOP_OUT_PIN (2)
#define ROLLING_SHUTTER_DOOR_OFF_OUT_PIN  (3)

#define EMERGENCY_STOP_IN_PIN (1)

double mm2PulseForX(double value);
double Pulse2mmForX(double value);

double mm2PulseForY(double value);
double Pulse2mmForY(double value);

class servoControl
{
public:
    servoControl();
    ~servoControl();

    bool connectCtrler(int &axisNum);
    bool enableAllAxis(bool toEnable);
    bool runBackZero(int axis);
    bool runMoveHome();
    bool runSingleAxis(int axis, int pulses, int posMode);
    bool getPosition(int axis, double &pos);
    void clearErr();
    void stopMove();
    int set_output_pin(int pin_num, int value);
    int get_input_pin(int pin_num, int& value);
    int close();
    int reset();
    int getConnectStatus();

    bool runMoveHomeX();
    bool runMoveHomeY();

    PINGGER pingger;
    bool mCheckZeroInit = true;


private:
    void print_base_info();
    void loadINI();

    char servoIP[16]={};

    WORD   m_nConnectNo = 0;

    unsigned long TotalAxis = 0;                                                //总轴数

    bool is_Enable[2] = { false, false};                                        // 使能状态
    bool is_ZeroInit[2] = { false, false };                                     // 是否完成回零
    const int zero_mode[2] = { 19, 21 };                                        // 回零模式 ok

    const int zero_speed = 20000;                                               // 回零高速
    int move_speed[2] = {5000/2, 5000};                                         // 移动高速

    int x_home_pos = 0;                                                          // 启动后准备姿态
    bool mInited =false;
    const int x_max_mm = -25000;
    const int y_max_mm = 450;
    int x_max=25000;
    int y_max=0;
};

#endif // SERVOCONTROL_H
